<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
      <el-form-item label="人员姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入人员姓名" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:role:add']">新增</el-button>
      </el-col>
       
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:role:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="personnelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" width="180" />
      <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="年龄" prop="age" :show-overflow-tooltip="true" width="80" />
      <el-table-column label="性别" prop="sex" :show-overflow-tooltip="true" width="80" />
      <el-table-column label="案件编号" prop="caseNumber" :show-overflow-tooltip="true" width="200" show-password />
      <el-table-column label="案件受理时间" prop="caseAcceptanceTime" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="案件结案时间" prop="caseClosureTime"  width="150" />
      <el-table-column label="案件类别" prop="caseType" width="150" />
      <el-table-column label="受害人情况描述" prop="description" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="修改" placement="top" v-if="scope.row.id !== 1">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:role:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="scope.row.id !== 1">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:role:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人员配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入人员姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择" style="width: 100%;">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" placeholder="请输入人员年龄" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="案件编号" prop="caseNumber">
          <el-input v-model="form.caseNumber" placeholder="请输入案件编号" />
        </el-form-item>
        <el-form-item label="案件受理时间" prop="caseAcceptanceTime">
          <el-date-picker type="date" v-model="form.caseAcceptanceTime" placeholder="请输入案件受理时间" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"/>
        </el-form-item>
        <el-form-item label="案件结案时间" prop="caseClosureTime">
          <el-date-picker type="date" v-model="form.caseClosureTime" placeholder="请输入案件结案时间" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"/>
        </el-form-item>
        <el-form-item label="案件类别" prop="caseType">
          <el-input v-model="form.caseType" placeholder="请输入案件类别" />
        </el-form-item>
        <el-form-item label="受害人情况描述" prop="description">
          <el-input type="textarea" v-model="form.description" placeholder="请输入受害人情况" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Victim">
import { addPersonnel, delPersonnel, listPersonnel, updatePersonnel } from "@/api/system/personnel";
import useUserStore from '@/store/modules/user'

const { proxy } = getCurrentInstance();

const personnelList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    name: undefined,
    type:false
  },
  rules: {
    name: [{ required: true, message: "人员姓名不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询人员列表 */
function getList() {
  loading.value = true;
  listPersonnel(queryParams.value).then(res => {
    // proxy.$modal.msgSuccess("查询成功");
    const users = res.result
    personnelList.value = users.records;
    total.value = users.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  if (useUserStore().roles.includes("admin")) {
    let _ids = (row.id || ids.value).toString().split();
    proxy.$modal.confirm('是否确认删除人员编号为"' + _ids + '"的数据项?').then(function () {
      return delPersonnel(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
  } else {
    proxy.$modal.msgError("暂无权限");
  }
    
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    sex: undefined,
    caseNumber: undefined,
    caseAcceptanceTime: undefined,
    caseClosureTime: undefined,
    caseType: undefined,
    description: undefined,
  };
  proxy.resetForm("roleRef");
}

/** 添加人员 */
function handleAdd() {
  if (useUserStore().roles.includes("admin")) {
    reset();
    open.value = true;
    title.value = "添加人员";
  } else {
    proxy.$modal.msgError("暂无权限");
  }
}

/** 修改人员 */
function handleUpdate(row) {
  reset();
  if (useUserStore().roles.includes("admin")) {
    let data = toRaw(personnelList.value.filter(e => e.id == row.id)[0]);
    form.value = data;
    open.value = true;
    title.value = "修改人员";
  } else {
    proxy.$modal.msgError("暂无权限");
  }
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate(valid => {
    if (valid) {
      form.value.type = false;
      if (form.value.id != undefined) {
        updatePersonnel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPersonnel(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>
